<?php

namespace component\system\system_auth;

use common\ApiError;
use common\logic\Logic;
use think\exception\ValidateException;

class SystemAuth extends Logic
{
    const LEVEL_FIRST = 1;
    const LEVEL_SECOND = 2;
    const LEVEL_THIRD = 3;
    /**
     * @var array
     */
    public $fields = [
        'id',
        'pid',
        'title',
        'path',
        'desc',
        'level',
        'create_time',
    ];

    public $descFields = [
        'id',
        'title'
    ];

    /**
     * @return SystemAuthModel|\think\Model
     */
    protected function model()
    {
        return new SystemAuthModel();
    }

    /**
     * @param $title
     * @return bool|mixed|string
     */
    public function postModule($title)
    {
        $title = trim($title);
        $title = trim($title, '/');

        $params = ['title' => $title];

        $record = $this->find($params);

        if ($record) {
            ApiError::instance()->message('请不要重复添加模块');
            return false;
        }

        $params['level'] = self::LEVEL_FIRST;

        return $this->save($params);
    }

    /**
     * @param int $level
     * @return array|\think\Collection|\think\Paginator
     */
    public function getByLevel($level = self::LEVEL_FIRST)
    {
        return $this->setFields($this->descFields)->select(['level' => $level]);
    }

    /**
     * @return array|\think\Collection|\think\Paginator
     */
    public function first()
    {
        return $this->setFields($this->descFields)->select(['level' => self::LEVEL_FIRST]);
    }

    /**
     * @return array|\think\Collection|\think\Paginator
     */
    public function second()
    {
        return $this->setFields($this->descFields)->select(['level' => self::LEVEL_SECOND]);
    }

    /**
     * @param $path
     * @return SystemAuthModel|null|array
     */
    public function getByPath($path)
    {
        return $this->find(
            [
                'path' => $path,
            ]
        );
    }

    /**
     * @param $data
     * @return bool
     */
    public function postRestful($data)
    {
        try {
            validate(SystemAuthValidate::class)->scene('restful')->check($data);
        } catch (ValidateException $exception) {
            ApiError::instance()->message($exception->getMessage());
            return false;
        }

        $data['title'] = trim($data['title']);

        /**
         * @var SystemAuthModel $record
         */
        $record = $this->find(
            [
                'pid' => $data['pid'],
                'title' => $data['title'],
            ]
        );

        if (empty($record)) {
            $data['level'] = self::LEVEL_SECOND;
            $id = $this->save($data, 'postRestful');
        } else {
            $id = $record->id;
        }

        $route = $data['route'] ?? '';

        if ($route) {

            $restful = [
                'get' => '查看',
                'post' => '新增',
                'put' => '编辑',
                'patch' => '修改',
                'delete' => '添加到回收站',
                'recycle' => '查看回收站',
                'recovery' => '恢复',
                'clear' => '删除',
            ];

            $lists = [];
            foreach ($restful as $key => $item) {
                $lists[] = [
                    'pid' => $id,
                    'title' => $item,
                    'path' => 'system/' . $route . '/' . $key,
                    'level' => self::LEVEL_THIRD
                ];
            }

            foreach ($lists as $item) {
                if ($this->getByPath($item['path'])) {
                    continue;
                }
                $this->save($item);
            }
        }

        return true;
    }
}